package com.example.service;

import com.example.mapper.JobIntentionMapper;
import com.example.mapper.JobSeekerMapper;
import com.example.mapper.LearningExperienceMapper;
import com.example.mapper.WorkExperienceMapper;
import com.example.pojo.bo.JobSeekerBO;
import com.example.pojo.bo.LearningExperienceBO;
import com.example.pojo.bo.WorkExperienceBO;
import com.example.pojo.dto.SearchDTO;
import com.example.pojo.dto.SearchWordDTO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 本文来演示:
 *
 * @author 赵建云
 * @create 2023/10/4
 */
@Service
public class SearchServiceImpl implements SearchService {
    @Autowired
    private LearningExperienceMapper learningExperienceMapper;
    @Autowired
    private JobSeekerMapper jobSeekerMapper;
    @Autowired
    private WorkExperienceMapper workExperienceMapper;
    @Autowired
    private JobIntentionMapper jobIntentionMapper;


    @Override
    public List<SearchDTO> searchAll() {
        List<SearchDTO> searchDTOList = new ArrayList<>();
        List<JobSeekerBO> jobSeekerBOList = jobSeekerMapper.searchAll();
        for (JobSeekerBO jobSeekerBO : jobSeekerBOList) {
            Integer id = jobSeekerBO.getId();
            LearningExperienceBO learningExperienceBO = learningExperienceMapper.searchAll(id);
            WorkExperienceBO workExperienceBO = workExperienceMapper.search(id);
            SearchDTO searchDTO = new SearchDTO();
            searchDTO.setId(id);
            searchDTO.setJsName(jobSeekerBO.getJsName());
            searchDTO.setAvatar(jobSeekerBO.getAvatar());
            searchDTO.setAge(jobSeekerBO.getAge()+"周岁");
            if (learningExperienceBO!=null){
                if (learningExperienceBO.getBackground()==1){
                    searchDTO.setBackground("本科");
                }else if (learningExperienceBO.getBackground()==2){
                    searchDTO.setBackground("硕士");
                }else if (learningExperienceBO.getBackground()==3){
                    searchDTO.setBackground("博士");
                }
                if (learningExperienceBO.getScType()==1){
                    searchDTO.setScType("统招本科");
                }else if (learningExperienceBO.getScType()==2){
                    searchDTO.setScType("双一流");
                }else if (learningExperienceBO.getScType()==3){
                    searchDTO.setScType(("211院校"));
                } else if (learningExperienceBO.getScType()==4) {
                    searchDTO.setScType("985院校");
                } else if (learningExperienceBO.getScType()==5) {
                    searchDTO.setScType("留学生");
                }
                searchDTO.setScName(learningExperienceBO.getScName());
            }


            if (jobSeekerBO!=null){
                if (jobSeekerBO.getJsSex().equals("1")){
                    searchDTO.setJsSex("男");
                }else {
                    searchDTO.setJsSex("女");
                }
                if (jobSeekerBO.getJobSeek().equals("1")){
                    searchDTO.setJobSeek("求职");
                }else {
                    searchDTO.setJobSeek("未求职");
                }
            }
            if (workExperienceBO!=null){
                if (workExperienceBO.getExperience()==0){
                    searchDTO.setExperience("应届");
                }else {
                    searchDTO.setExperience("已工作"+workExperienceBO.getExperience()+"年");
                }
                searchDTO.setJobContent(workExperienceBO.getJobContent());
            }


            searchDTOList.add(searchDTO);
        }
        return searchDTOList;
    }

    @Override
    public List<SearchDTO> searchByWord(SearchWordDTO searchWordDTO) {
        List<SearchDTO> searchDTOList = new ArrayList<>();
        List<Integer> searchList1 = new ArrayList<>();
        List<Integer> searchList = new ArrayList<>();
        Integer startAge = 0;
        Integer endAge = 0;
        Integer startExperience= 0;
        Integer endExperience = 0;
        String search = searchWordDTO.getSearch();
        String background = searchWordDTO.getBackground();
        String scType = searchWordDTO.getScType();
        String age = searchWordDTO.getAge();
        String[] ageStr = age.split("-");
        if (!age.equals("0")){
            startAge = Integer.valueOf(ageStr[0]);
            endAge = Integer.valueOf(ageStr[1]);
        }
        String experience = searchWordDTO.getExperience();
        String[] experienceStr = experience.split("-");
        if (!experience.equals("0")){
            startExperience = Integer.valueOf(experienceStr[0]);
            endExperience = Integer.valueOf(experienceStr[1]);
        }
        if (search.equals("")){
            searchList1 = jobSeekerMapper.searchAllId();
        }else {
            if (search.equals("本科")){
                search= String.valueOf(1);
            }else if (search.equals("硕士")){
                search=String.valueOf(2);
            }else {
                search=String.valueOf(3);
            }
            List<Integer> list = learningExperienceMapper.searchByWord(search);
            List<Integer> list2 = workExperienceMapper.searchByWord(search);
            List<Integer> list3 = jobIntentionMapper.searchByWord(search);
            HashSet<Integer> hashSet = new HashSet<>();
            hashSet.addAll(list);
            hashSet.addAll(list2);
            hashSet.addAll(list3);
            searchList1 = new ArrayList<>(hashSet);
            searchList1.forEach(System.out::println);
        }

        List<Integer> listAge = jobSeekerMapper.searchByAge(startAge,endAge);
        List<Integer> listBackground = learningExperienceMapper.searchByBackground(Integer.valueOf(background));
        List<Integer> listExperience = workExperienceMapper.searchByExperience(startExperience,endExperience);
        List<Integer> listScType = learningExperienceMapper.searchByType(Integer.valueOf(scType));
        Set<Integer> setList = new HashSet<>(listAge);
        setList.retainAll(searchList1);
        setList.retainAll(listExperience);
        setList.retainAll(listBackground);
        setList.retainAll(listScType);
        searchList = new ArrayList<>(setList);

        for (Integer jsId : searchList) {
            JobSeekerBO jobSeekerBO = jobSeekerMapper.searchByWord(jsId);
            LearningExperienceBO learningExperienceBO = learningExperienceMapper.searchAll(jsId);
            WorkExperienceBO workExperienceBO = workExperienceMapper.search(jsId);
            SearchDTO searchDTO = new SearchDTO();
            searchDTO.setId(jobSeekerBO.getId());
            searchDTO.setAge(jobSeekerBO.getAge()+"周岁");
            if (jobSeekerBO.getJsSex()==0){
                searchDTO.setJsSex("女");
            }else {
                searchDTO.setJsSex("男");
            }
            searchDTO.setAvatar(jobSeekerBO.getAvatar());
            if (jobSeekerBO.getJobSeek()==0){
                searchDTO.setJobSeek("未求职");
            }else {
                searchDTO.setJobSeek("已求职");
            }
            if (learningExperienceBO.getScType()==1){
                searchDTO.setScType("统招本科");
            }else if (learningExperienceBO.getScType()==2){
                searchDTO.setScType("双一流");
            }else if (learningExperienceBO.getScType()==3){
                searchDTO.setScType(("211院校"));
            } else if (learningExperienceBO.getScType()==4) {
                searchDTO.setScType("985院校");
            } else if (learningExperienceBO.getScType()==5) {
                searchDTO.setScType("留学生");
            }
            if (workExperienceBO.getExperience()==0){
                searchDTO.setExperience("应届");
            }else {
                searchDTO.setExperience("已工作"+workExperienceBO.getExperience()+"年");
            }
            searchDTO.setJsName(jobSeekerBO.getJsName());
            searchDTO.setScName(learningExperienceBO.getScName());
            searchDTO.setJobContent(workExperienceBO.getJobContent());
            if (learningExperienceBO.getBackground()==1){
                searchDTO.setBackground("本科");
            }else if (learningExperienceBO.getBackground()==2){
                searchDTO.setBackground("硕士");
            }else if (learningExperienceBO.getBackground()==3){
                searchDTO.setBackground("博士");
            }

            searchDTOList.add(searchDTO);
        }
        return searchDTOList;
    }
}
